package com.webamc.config;

import com.webamc.common.enums.Params;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

/**
 * User: Choti
 * Date: Apr 6, 2009
 * Time: 8:32:52 PM
 */
public class StaticData {
    private static StaticData self;
    private Map<String, Set<String>> oneD;
    private Map<String, String[][]> twoD;

    public static synchronized StaticData instance() {
        return self;
    }

    public void setOneD(Map<String, String> m) {
        final String DELIMROW = m.remove(Params.DELIMROW);

        self = this; //capture the Spring bean for Singleton pattern
        oneD = new HashMap<String, Set<String>>(m.size());
        for (Map.Entry<String, String> e : m.entrySet()) {
            Set<String> set = new LinkedHashSet<String>(); //UNIQUE VALUE AND SEQUENCE is imp
            String[] s = e.getValue().split(DELIMROW);
            for (String v : s) {
                set.add(v.trim().toUpperCase()); //NEUTRALIZE SPACES AND CASE
            }
            ////
            oneD.put(e.getKey(), set);
        }
    }

    public void setTwoD(Map<String, String> m) {
        final String DELIMROW = m.remove(Params.DELIMROW);
        final String DELIMCOL = m.remove(Params.DELIMCOL);

        self = this; //capture the Spring bean for Singleton pattern
        twoD = new HashMap<String, String[][]>(m.size());
        for (Map.Entry<String, String> e : m.entrySet()) {
            String[] rows = e.getValue().split(DELIMROW);
            String[][] table = new String[rows.length][];
            for (int i = 0; i < rows.length; i++) {
                String[] cols = rows[i].split(DELIMCOL);
                for (int j = 0; j < cols.length; j++) {
                    cols[j] = cols[j].trim().toUpperCase(); //NEUTRALIZE SPACES AND CASE
                }
               table[i] = cols;
            }
            ////
            twoD.put(e.getKey(), table);
        }
    }

    public Set<String> getOneD(String key) {
        return oneD.get(key);
    }
    
    public String[][] getTwoD(String key) {
        return twoD.get(key);
    }
}
